import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { HabitService } from '../../service/habit.service';
import { Wish } from './wish'
import { Plan } from '../plan/plan';

@Component({
  selector: 'page-wish',
  templateUrl: 'wishPage.html',
})
export class WishPage {
  wishList : Array<Wish>;
  planList : Array<Plan>;
  isOpen : Array<boolean>;
  constructor(public navCtrl: NavController, public habitService : HabitService, public navParams: NavParams) {
    this.planList = [];
    this.wishList = [];
    this.isOpen = [];
  }
  
  ngOnInit() : void{
    this.getWish();
    this.getPlan();
    for(let i = 0;i<this.wishList.length;i++){
      this.isOpen.push(false);
    }
  }
  addWish(){
    this.habitService.addWish().then(
      ()=>{this.getWish();}
    );
  }
  getWish(){
    this.habitService.getWish().then(
      wishList=>{
      this.wishList=wishList;
      this.habitService.wishList = this.wishList;
    }
    );
  }
  getPlan(){
    this.habitService.getPlan().then(
      planList=>{
      this.planList=planList;
      this.habitService.planList = this.planList;
    }
    );
  }
  openPlanList(i){
    if(this.isOpen[i]==false){
      this.isOpen[i] = true;
    }
    else{
      this.isOpen[i] = false;
    }
  }
  findPlan(pID){
    for(let i = 0; i < this.planList.length; i++){
      if(this.planList[i].pID == pID){
        return this.planList[i].pName;
      }
    }
  }


}
